package com.waijiaojun.tpo.web.baidu;

import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.BaiduYuyinService;
import com.waijiaojun.tpo.service.UserService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.JsonUtil;
import com.waijiaojun.tpo.utils.UserUtil;
import com.waijiaojun.tpo.vo.IndexData;
import com.waijiaojun.tpo.vo.baidu.BaiduApiYuyinVo;
import com.waijiaojun.tpo.web.IndexController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Peter on 2018/4/11.
 */
@Controller
@RequestMapping("/baidu")
public class BaiduYuYinController {
    private static Logger logger = LoggerFactory.getLogger(BaiduYuYinController.class);

    @Autowired
    private BaiduYuyinService baiduYuyinService;

    @RequestMapping("api/yuyin/token")
    @ResponseBody
    public RestResponse indexData() {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in);
            BaiduApiYuyinVo data = baiduYuyinService.receiveToken();
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

}
